package metier;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class ihm extends JFrame {

	private JPanel contentPane;
	private JTextField textNomCollab;
	private JTextField textPrenomCollab;
	private JTextField textObjet;
	private JTextField textClient;
	private JTextField textLibelleTaches;
	private JTextField textChargePreviTaches;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ihm frame = new ihm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ihm() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 412, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		contentPane.add(tabbedPane);
		
		JLayeredPane PanneauCollab = new JLayeredPane();
		tabbedPane.addTab("Collaborateurs", null, PanneauCollab, null);
		
		JLabel lblListeDesCollaborateurs = new JLabel("Liste des collaborateurs");
		lblListeDesCollaborateurs.setBounds(10, 11, 159, 14);
		PanneauCollab.add(lblListeDesCollaborateurs);
		
		String[] collaborateurs = new String[4];
			Connection maCnx = null;
			try {
				   // on charge le bon pilote JDBC
				   Class.forName("com.mysql.jdbc.Driver") ;
				   System.out.println("Pilote JDBC pour MySql chargé");

					// établissement de la connexion
				   maCnx = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/tempofixe_slam2g5", "root", "");
				   Statement instruction = (Statement) maCnx.createStatement();
				   
				   ResultSet resultat = instruction.executeQuery("SELECT NOM FROM collaborateur");
				   
				   int i = 0;
					while (resultat.next()) {
						collaborateurs[i] = resultat.getString("NOM");
						i++;
				      }
			}
			catch (ClassNotFoundException e) {
				System.err.println("Erreur chargement pilote JDBC pour MySql : "+ e.getMessage());
			}
			catch (SQLException e) {
				System.err.println("Erreur ouverture connexion vers le serveur MySql : " + e.getMessage());
			}
		
		//String[] collaborateurs = { "John", "Jack", "Jean",
                //"Joe", "Joey", "Jules" };
				
		JComboBox cbxLstCollab = new JComboBox(collaborateurs);
		cbxLstCollab.setBounds(10, 36, 122, 20);
		PanneauCollab.add(cbxLstCollab);
		
		JButton btnSupprCollab = new JButton("suppression");
		btnSupprCollab.setBounds(142, 35, 106, 23);
		PanneauCollab.add(btnSupprCollab);
		
		JButton btnActuCollab = new JButton("actualisation");
		btnActuCollab.setBounds(258, 35, 114, 23);
		PanneauCollab.add(btnActuCollab);
		
		JButton btnAjouterCollab = new JButton("ajouter");
		btnAjouterCollab.setBounds(283, 190, 89, 23);
		PanneauCollab.add(btnAjouterCollab);
		
		textNomCollab = new JTextField();
		textNomCollab.setBounds(69, 67, 86, 20);
		PanneauCollab.add(textNomCollab);
		textNomCollab.setColumns(10);
		
		JLabel lblNom = new JLabel("Nom : ");
		lblNom.setBounds(10, 70, 46, 14);
		PanneauCollab.add(lblNom);
		
		JLabel lblPrnom = new JLabel("Pr\u00E9nom :");
		lblPrnom.setBounds(10, 95, 67, 14);
		PanneauCollab.add(lblPrnom);
		
		textPrenomCollab = new JTextField();
		textPrenomCollab.setBounds(69, 92, 86, 20);
		PanneauCollab.add(textPrenomCollab);
		textPrenomCollab.setColumns(10);
		
		JLayeredPane PanneauContrats = new JLayeredPane();
		tabbedPane.addTab("Contrats", null, PanneauContrats, null);
		
		JLabel lblListeDesContrats = new JLabel("Liste des contrats");
		lblListeDesContrats.setBounds(10, 11, 111, 14);
		PanneauContrats.add(lblListeDesContrats);
		
		String[] contrats = { "Contrat 1", "Contrat 2", "Contrat 3",
                "Contrat 4", "Contrat 5", "Contrat 6" };
		
		JComboBox cbxLstContrats = new JComboBox(contrats);
		cbxLstContrats.setBounds(10, 36, 118, 20);
		PanneauContrats.add(cbxLstContrats);
		
		JButton btnSupprContrats = new JButton("suppression");
		btnSupprContrats.setBounds(138, 35, 112, 23);
		PanneauContrats.add(btnSupprContrats);
		
		JButton btnActuContrats = new JButton("actualisation");
		btnActuContrats.setBounds(260, 35, 111, 23);
		PanneauContrats.add(btnActuContrats);
		
		textObjet = new JTextField();
		textObjet.setBounds(60, 67, 86, 20);
		PanneauContrats.add(textObjet);
		textObjet.setColumns(10);
		
		textClient = new JTextField();
		textClient.setBounds(60, 95, 86, 20);
		PanneauContrats.add(textClient);
		textClient.setColumns(10);
		
		JLabel lblObjet = new JLabel("Objet :");
		lblObjet.setBounds(10, 70, 46, 14);
		PanneauContrats.add(lblObjet);
		
		JLabel lblClient = new JLabel("Client :");
		lblClient.setBounds(10, 98, 46, 14);
		PanneauContrats.add(lblClient);
		
		JButton btnAjouterContrats = new JButton("Ajouter");
		btnAjouterContrats.setBounds(282, 190, 89, 23);
		PanneauContrats.add(btnAjouterContrats);
		
		JLayeredPane PanneauTaches = new JLayeredPane();
		tabbedPane.addTab("Tâches", null, PanneauTaches, null);
		
		JComboBox cbxLstTaches = new JComboBox(new Object[]{});
		cbxLstTaches.setBounds(10, 34, 118, 20);
		PanneauTaches.add(cbxLstTaches);
		
		JButton btnSupprTaches = new JButton("suppression");
		btnSupprTaches.setBounds(139, 32, 112, 23);
		PanneauTaches.add(btnSupprTaches);
		
		JButton btnActuTaches = new JButton("actualisation");
		btnActuTaches.setBounds(260, 33, 111, 23);
		PanneauTaches.add(btnActuTaches);
		
		textLibelleTaches = new JTextField();
		textLibelleTaches.setColumns(10);
		textLibelleTaches.setBounds(101, 65, 86, 20);
		PanneauTaches.add(textLibelleTaches);
		
		textChargePreviTaches = new JTextField();
		textChargePreviTaches.setColumns(10);
		textChargePreviTaches.setBounds(101, 96, 86, 20);
		PanneauTaches.add(textChargePreviTaches);
		
		JLabel lblLibell = new JLabel("Libell\u00E9 :");
		lblLibell.setBounds(10, 67, 46, 14);
		PanneauTaches.add(lblLibell);
		
		JLabel lblChargePrvi = new JLabel("Charge pr\u00E9vi. :");
		lblChargePrvi.setBounds(10, 99, 91, 14);
		PanneauTaches.add(lblChargePrvi);
		
		JButton btnAjouterTaches = new JButton("Ajouter");
		btnAjouterTaches.setBounds(282, 190, 89, 23);
		PanneauTaches.add(btnAjouterTaches);
		
		JLabel lblListeDesTches = new JLabel("Liste des t\u00E2ches");
		lblListeDesTches.setBounds(10, 11, 117, 14);
		PanneauTaches.add(lblListeDesTches);
		
		JLabel lblListeMatricule = new JLabel("Collaborateurs");
		lblListeMatricule.setBounds(10, 136, 91, 14);
		PanneauTaches.add(lblListeMatricule);
		
		JLabel lblCollaborateurs = new JLabel("Contrats");
		lblCollaborateurs.setBounds(10, 161, 104, 14);
		PanneauTaches.add(lblCollaborateurs);
		
		JComboBox cbxCollabTaches = new JComboBox(collaborateurs);
		cbxCollabTaches.setBounds(101, 127, 86, 20);
		PanneauTaches.add(cbxCollabTaches);
		
		JComboBox cbxContratsTaches = new JComboBox();
		cbxContratsTaches.setBounds(101, 158, 86, 20);
		PanneauTaches.add(cbxContratsTaches);
		
		String[] taches = { "Tache 1", "Tache 2", "Tache 3",
                "Tache 4", "Tache 5", "Tache 6" };
	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
